<?php
 if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcoupon extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getData()
    {
        $index = $this->getParamInt('index');
        if(!in_array($index, array(null, '', '0')))
        {
            $limit_sql = " LIMIT  ".$index.", 10";
        }else{
            $limit_sql = " LIMIT  10";
        }
        
        $sql = 'SELECT ecp.id AS ecpid, ecp.sale_off, ecp.amount,ecp.type_sl,  FROM_UNIXTIME(ecp.date_user, "%d/%m/%Y") AS expiration_date, ecp.`condition` FROM ecoupon AS ecp WHERE ecp.active = 1';
        
		$num_rows = $this->db->query($sql.$limit_sql)->num_rows();
		if($num_rows == 0)
		{
			return (object)array('status' => 404,'content' => array(), 'description' => 'Danh sach coupon rong');
		}
        $runSql = $this->db->query($sql.$limit_sql)->result_object();
        
        foreach($runSql AS $el=>$le)
        {
            if(!in_array($le->ecpid, array(null, '', '0')) && $le->type_sl != 0)
            {
                //die('SELECT CONCAT(r.`name`,": ",ra.address) AS address FROM ecoupon_key AS eck INNER JOIN restaurant AS r ON r.id = eck.id_restaurant INNER JOIN restaurant_address AS ra ON ra.restaurant_id = r.id WHERE eck.id_ecoupon = '.$le->ecpid);
                $runSql[$el]->_address = $this->db->query('SELECT CONCAT(r.`name`,": ", ra.address) AS address FROM ecoupon_key AS eck  INNER JOIN restaurant AS r ON r.id = eck.id_restaurant RIGHT JOIN restaurant_address AS ra ON ra.restaurant_id = r.id WHERE eck.id_ecoupon = '.$le->ecpid)->result_object();
            }else{
                $runSql[$el]->_address = (object)array();
            }
        }
        return (object)array('status' => 200,'content' => $runSql, 'description' => 'Danh sach coupon');
    }
    
    public function getDetail()
    {
        $id = $this->getParamInt('id');
        
        $sql = 'SELECT ecp.id AS ecpid, ecp.code_coupon, ecp.amount_available, ecp.content, ecp.content_sale_off AS brief, ecp.sale_off, ecp.amount,ecp.type_sl,  FROM_UNIXTIME(ecp.date_user, "%d/%m/%Y") AS expiration_date, ecp.`condition` FROM ecoupon AS ecp WHERE ecp.active = 1 AND ecp.id = '.$id;
        
		$num_rows = $this->db->query($sql)->num_rows();
		if($num_rows == 0)
		{
			return (object)array('status' => 404,'content' => array(), 'description' => 'Khong ton tai coupon nay');
		}
        $runSql = $this->db->query($sql)->result_object();
        
        foreach($runSql AS $el=>$le)
        {
            if(!in_array($le->ecpid, array(null, '', '0')) && $le->type_sl != 0)
            {
                //die('SELECT CONCAT(r.`name`,": ",ra.address) AS address FROM ecoupon_key AS eck INNER JOIN restaurant AS r ON r.id = eck.id_restaurant INNER JOIN restaurant_address AS ra ON ra.restaurant_id = r.id WHERE eck.id_ecoupon = '.$le->ecpid);
                $runSql[$el]->_address = $this->db->query('SELECT CONCAT(r.`name`,": ", ra.address) AS address FROM ecoupon_key AS eck  INNER JOIN restaurant AS r ON r.id = eck.id_restaurant RIGHT JOIN restaurant_address AS ra ON ra.restaurant_id = r.id WHERE eck.id_ecoupon = '.$le->ecpid)->result_object();
            }else{
                $runSql[$el]->_address = (object)array();
            }
        }        
        
        
        return (object)array('status' => 200,'content' => $runSql, 'description' => 'Chi tiet coupon');
    }
    
    public function reviewCouponDB($coupon_id,$user_id,$name,$star,$content){
        
        $check = $this->db->query('SELECT id FROM coupon_user WHERE id_coupon = '.$coupon_id.' AND id_user = '.$user_id)->num_rows();
        if($check > 0)
        {
            $checkUser = $this->db->query('SELECT id FROM member WHERE id = '.$user_id.' AND active = 2')->num_rows();
            if($checkUser > 0)
            {
                return (object)array('status' => 203, 'description' => 'Tai khoan nay dang bi khoa');
            }
            
            $data = array (
                'coupon_id'     => $coupon_id,
                'user_id'       => $user_id,
                'name'          => $name,
                'star'          => $star,
                'content'       => $content,
                'created'       => time(),
                'improved'      => 0
                );
                
            $run = $this->db->insert('ecoupon_review', $data);
            if($run){
                return (object)array('status' => 200, 'description' => 'Insert thanh cong');
            }else{
                return (object)array('status' => 201, 'description' => 'Insert that bai');
            }
        }else{
            return (object)array('status' => 202, 'description' => 'User chua su dung coupon nay');
        }
        
    }
    
    public function getReviewCoupon(){
        
        $id = $this->getParamInt('id');
        $index = $this->getParamInt('index');
        if(!in_array($index, array(null, '', '0')))
        {
            $limit_sql = " LIMIT  ".$index.", 10";
        }else{
            $limit_sql = " LIMIT  10";
        }
        
        $sql = $this->db->query('SELECT er.`name`, er.star, er.content, m.username, m.fullname FROM ecoupon_review AS er INNER JOIN member AS m ON er.user_id = m.id WHERE er.improved = 1 AND er.coupon_id = '.$id.$limit_sql);
		$data = $sql->result_object();
		$num_rows = $sql->num_rows();
		if($num_rows == 0)
		{
			return (object)array('status' => 404,'content' => array(), 'description' => 'Khong ton tai danh sách comment cho coupon nay');
		}
        return (object)array('status' => 200,'content' => $data, 'description' => 'Danh sach coupon');
    }


}
